<?php

/**
 * advertisement actions.
 *
 * @package    GREEN_LIGHT
 * @subpackage advertisement
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class advertisementActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
	public function executeIndex(sfWebRequest $request)
	{
		$this->ads_form = new AdminAdvertisementForm();
		if ($request->hasParameter('create') && $request->getParameter('create') == 'Create') {
			$res = $request->getParameter('ads');
			$this->is_create_action = true;
			if ($res['link'] != '') {
				$ads = new GlAdvertisement();
				$c = Doctrine_Core::getTable('GlAdvertisement')->findOneByLink($res['link']);
				if (!$c) {
					$ads->name = $res['name'];
					$ads->link = $res['link'];
					$ads->display_position = $res['display_position'];
					if ($res['is_active'] == 'no')
						$ads->is_active = false;
					else
						$ads->is_active = true;
					$ads->content = $res['content'];
					$ads->position = $res['position'];
					$ads->created_at = date(DATE_ISO8601);
					$ads->updated_at = date(DATE_ISO8601);
					try {
						$ads->save();
						$this->create_success = true;
					} catch (sfException $e) {
						echo $e->getMessage();
					}
					
					try {
						$files = $this->getRequest()->getFiles();
						$file = $files['ads']['picture'];
						if ($file['size'] > 0)
						{
							$tmpName = $file['tmp_name'];
							$fileName = $file['name'];
							$fileExt = trim(strrchr($fileName,'.'));
			
							$img = new sfImage($tmpName, $file['type']);
							$img->saveAs(sfConfig::get('sf_upload_dir').'/ads/'.$fileName, $file['type']);
							
							$img1 = new sfImage(sfConfig::get('sf_upload_dir').'/ads/'.$fileName, $file['type']);
							$fileName1 = ($ads->advertisement_id).$fileExt;
							$img1->thumbnail(170,235,'center');
							$img1->setQuality(100);
							$img1->saveAs(sfConfig::get('sf_upload_dir').'/ads/'.$fileName1, $file['type']);
							
							$img3 = new sfImage(sfConfig::get('sf_upload_dir').'/ads/'.$fileName, $file['type']);
							$fileName2 = "slide_".$fileName1;
							$img3->thumbnail(154,234,'center');
							$img3->setQuality(100);
							$img3->saveAs(sfConfig::get('sf_upload_dir').'/ads/'.$fileName2, $file['type']);
							
							$ads->picture = $fileName1;
							$ads->save();
							$this->upload_success = true;
						}
					} catch (sfException $e) {
						//echo $e->getMessage();
					}
					
				} else {
					$this->create_success = false;
				}
			}
			
		} else {
			$q = Doctrine_Query::create()
				->from('GlAdvertisement c');
	
			$res = $request->getParameter('ads');
			$q = $this->execFilers($q, $res);
			$this->sort_by = $request->getParameter('sort_by', 'created_at');
			if ($request->getParameter('sort_order', 1))
				$this->sort_order = 'desc';
			else
				$this->sort_order = 'asc';
			if (Doctrine_Core::getTable('GlAdvertisement')->hasField($this->sort_by))
				$q->orderBy('c.'.$this->sort_by.' '.strtoupper($this->sort_order));
			$this->pager = new sfDoctrinePager('GlAdvertisement', 20);
			$this->pager->setQuery($q);
			$this->pager->setPage($request->getParameter('page', 1));
			$this->pager->init();
			if ($request->isXmlHttpRequest()) {
				return 'Ajax';
			} else return SfView::SUCCESS;
		}	
	}
	
	private function execFilers($q, $res)
	{
		if ($res['name'] != '')
			$q->addWhere('c.name LIKE "%'.$res['name'].'%"');
		if ($res['link'] != '')
			$q->addWhere('c.link LIKE "%'.$res['link'].'%"');
		if ($res['link'] != '')
			$q->addWhere('c.link LIKE "%'.$res['link'].'%"');
		return $q;
	}
}
